//
//  PkUserInfoView.m
//  Learn
//
//  Created by apple on 2024/4/6.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "PkUserInfoView.h"

@interface PkUserInfoView ()
@property (nonatomic, strong) UIImageView    * bgImg1;

@property (nonatomic, strong) UIImageView    * userHeaderImg;
@property (nonatomic, strong) UILabel    * userName;
@property (nonatomic, strong) UILabel    * userSchool;


@property (nonatomic, strong) UILabel    * pkCountLabel;
@property (nonatomic, strong) UILabel    * pkCountNum;
@property (nonatomic, strong) UILabel    * winLabel;
@property (nonatomic, strong) UILabel    * winNum;
@property (nonatomic, strong) UILabel    * winProLabel;
@property (nonatomic, strong) UILabel    * winPro;

@property (nonatomic, strong) UIButton    * sureBtn;



@end

@implementation PkUserInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        [self requestData];
    }
    return self;
}
- (void)initUI {
    self.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0];
    [self addSubview:self.bgImg1];
    [self addSubview:self.userHeaderImg];
    [self addSubview:self.userName];
    [self addSubview:self.userSchool];
    
    [self addSubview:self.sureBtn];
    
    [self addSubview:self.winLabel];
    [self addSubview:self.winNum];
    
    [self addSubview:self.pkCountLabel];
    [self addSubview:self.pkCountNum];
    
    [self addSubview:self.winProLabel];
    [self addSubview:self.winPro];

//    NSLog(@"%@\n%@\n%@\n%@\n",lApp.useModel.username,lApp.useModel.nickname,lApp.useModel.avatar,lApp.useModel.score);
}
- (UIImageView *)bgImg1 {
    if(!_bgImg1){
        _bgImg1 = [[UIImageView alloc]initWithFrame:CGRectMake(18, 40, SCREEN_W - 36,1.26*(SCREEN_W - 36))];
        _bgImg1.contentMode = UIViewContentModeScaleAspectFill;
        _bgImg1.image = [UIImage imageNamed:@"pk_bg1"];
    }
    return _bgImg1;
}


- (UIImageView *)userHeaderImg {
    if(!_userHeaderImg){
        _userHeaderImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80,80)];
        _userHeaderImg.center = CGPointMake(SCREEN_W/2, self.bgImg1.top);
        _userHeaderImg.contentMode = UIViewContentModeScaleAspectFill;
        [_userHeaderImg sd_setImageWithURL:[NSURL URLWithString:lApp.useModel.avatar]placeholderImage:[UIImage imageNamed:@"testheader"]];
        
        [_userHeaderImg js_setRoundedCorners:UIRectCornerAllCorners radii:40 borderColor:[UIColor whiteColor] width:1];
    }
    return _userHeaderImg;
}
- (UILabel *)userName {
    if(!_userName){
        _userName = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 + self.userHeaderImg.bottom, 200, 28)];
        _userName.centerX = self.userHeaderImg.centerX;
        _userName.text = lApp.useModel.nickname;
        _userName.textColor = [UIColor blackColor];
        _userName.font = PingFangMediumFont(20);
        _userName.textAlignment = NSTextAlignmentCenter;
    }
    return _userName;
}

- (UILabel *)userSchool {
    if(!_userSchool){
        _userSchool = [[UILabel alloc]initWithFrame:CGRectMake(0, 10 + self.userName.bottom, 200, 20)];
        _userSchool.centerX = self.userHeaderImg.centerX;
        _userSchool.text = [NSString stringWithFormat:@"%@ %@",lApp.schoolName,lApp.gradeName];
        _userSchool.textColor = COLOR_NOT_SELECT;
        _userSchool.font = PingFangMediumFont(14);
        _userSchool.textAlignment = NSTextAlignmentCenter;
    }
    return _userSchool;
}


- (UIButton *)sureBtn {
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureBtn.frame = CGRectMake(self.bgImg1.left +20 , self.bgImg1.bottom -75, self.bgImg1.width - 40, 45);
        
        [_sureBtn setTitle:@"随机匹配" forState:UIControlStateNormal];
        
        [_sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _sureBtn.titleLabel.font  = PingFangMediumFont(18);

        _sureBtn.az_colors = @[(__bridge id)rgbHex(0x02A1F9, 1).CGColor,(__bridge id)rgbHex(0x29F19C,1).CGColor];
        _sureBtn.az_startPoint = CGPointMake(0, 0.5);
        _sureBtn.az_endPoint = CGPointMake(1, 0.5);
        [_sureBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_sureBtn addTarget:self action:@selector(sureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

- (UILabel *)winLabel {
    if(!_winLabel){
        _winLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bgImg1.width/3, 17)];
        _winLabel.bottom = self.sureBtn.top - 60;
        _winLabel.centerX = self.sureBtn.centerX;
        _winLabel.text = @"战胜数";
        _winLabel.textColor = COLOR_NOT_SELECT;
        _winLabel.font = PingFangMediumFont(12);
        _winLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _winLabel;
}
- (UILabel *)pkCountLabel {
    if(!_pkCountLabel){
        _pkCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.winLabel.top, self.winLabel.width, self.winLabel.height)];
        _pkCountLabel.right = self.winLabel.left;
        _pkCountLabel.text = @"做战数";
        _pkCountLabel.textColor = COLOR_NOT_SELECT;
        _pkCountLabel.font = PingFangMediumFont(12);
        _pkCountLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _pkCountLabel;
}

- (UILabel *)winProLabel {
    if(!_winProLabel){
        _winProLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.winLabel.right, self.winLabel.top, self.winLabel.width, self.winLabel.height)];
        _winProLabel.text = @"胜率";
        _winProLabel.textColor = COLOR_NOT_SELECT;
        _winProLabel.font = PingFangMediumFont(12);
        _winProLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _winProLabel;
}


- (UILabel *)winNum {
    if(!_winNum){
        _winNum = [[UILabel alloc]initWithFrame:CGRectMake(self.winLabel.left, self.winLabel.top - 50, self.winLabel.width, 40)];
//        _winNum.text = @"0";
        _winNum.textColor = COLOR_TOPIC_SELECT;
        _winNum.font = PingFangMediumFont(32);
        _winNum.textAlignment = NSTextAlignmentCenter;
    }
    return _winNum;
}

- (UILabel *)pkCountNum {
    if(!_pkCountNum){
        _pkCountNum = [[UILabel alloc]initWithFrame:CGRectMake(self.pkCountLabel.left, self.winNum.top, self.winNum.width, 40)];
//        _pkCountNum.text = @"0";
        _pkCountNum.textColor = COLOR_TOPIC_SELECT;
        _pkCountNum.font = PingFangMediumFont(32);
        _pkCountNum.textAlignment = NSTextAlignmentCenter;
    }
    return _pkCountNum;
}
- (UILabel *)winPro {
    if(!_winPro){
        _winPro = [[UILabel alloc]initWithFrame:CGRectMake(self.winNum.right, self.winNum.top, self.winNum.width, 40)];
//        _winPro.text = @"0%";
        _winPro.textColor = COLOR_TOPIC_SELECT;
        _winPro.font = PingFangMediumFont(32);
        _winPro.textAlignment = NSTextAlignmentCenter;
    }
    return _winPro;
}
#pragma mark   随机pk
- (void)sureBtnClicked:(UIButton *)sender {
    WS(WeakSelf)
    [PKRequest request_getPkMatchDataResult:^(PkMatchModel * _Nonnull model, BOOL isDone) {
        if (isDone) {
            if (WeakSelf.pkClickedBlock) {
                WeakSelf.pkClickedBlock(model);
            }
        }
    }];
   
}



- (void)requestData {
    WS(WeakSelf)
    [PKRequest request_getPkHomeDataResult:^(PkUserInfoModel * _Nonnull model, BOOL isDone) {
        [WeakSelf updateUIWithModel:model];
    }];
}
- (void)updateUIWithModel:(PkUserInfoModel *)model {
    self.winPro.text = [NSString stringWithFormat:@"%.2f%@",model.rate.floatValue,@"%"];
    self.pkCountNum.text = model.count;
    self.winNum.text = model.yesCount;

}
@end
